package com.github.littleantfly.dictionary;

import com.alibaba.fastjson.serializer.DictFieldSerializerFilter;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.github.littleantfly.dictionary.config.DictProperties;
import com.github.littleantfly.dictionary.service.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;

@Configuration

@ConditionalOnClass(FastJsonHttpMessageConverter.class)
public class FastjsonDictAutoConfiguration implements InitializingBean {
    private final static Logger logger = LoggerFactory.getLogger(FastjsonDictAutoConfiguration.class);
    private final FastJsonHttpMessageConverter converter;
    private final DictProperties dictProperties;
    private final DictService dictService;

    public FastjsonDictAutoConfiguration(FastJsonHttpMessageConverter converter, DictProperties dictProperties, DictService dictService) {
        this.converter = converter;
        this.dictProperties = dictProperties;
        this.dictService = dictService;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        converter.getFastJsonConfig().setSerializeFilters(new DictFieldSerializerFilter(dictService, dictProperties));
        if(logger.isDebugEnabled()) {
            logger.debug("init FastJsonDictAutoConfiguration");
        }
    }
}
